
using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;

//SVN Ref
using org.tmatesoft.svn.core;
using org.tmatesoft.svn.core.wc;

namespace Subsync.Core.Subversion
{
	
	
	public class SVNRemove : CommandBase
	{
		public java.io.File filepath;
		public SVNURL[] urlpath = new SVNURL[1];
		public bool WConly;
		
		public SVNRemove(string working_path)
		{
			Debug.WriteLine("SVN Remove std constructor");
			WConly = this.checkPath(working_path);
			if (WConly){
				filepath = new java.io.File(working_path);
				urlpath[0] = null;
			}
			else{
				filepath = null;
				urlpath[0] = SVNURL.parseURIEncoded(working_path);
			}
				
		}
		
		public SVNRemove()
		{
			// Empty for install app?
			Debug.WriteLine("SVN Remove empty constructor");
		}
		
		public override void Execute()
		{
			Debug.WriteLine("SVN Remove Execution");
					
			// Setup library
			Tools.setupLibrary();
			
			// Setup clients
			SVNClientManager cm = SVNClientManager.newInstance();
						
			try {
				if (WConly){
					SVNWCClient wcclient = cm.getWCClient();
					wcclient.doDelete(filepath,false,false);
				}
				else{
					SVNCommitClient commitclient = cm.getCommitClient();
					commitclient.doDelete(urlpath,"Deletion");
				}
					
			} catch (SVNException exptn) {
				throw new SVNExecuteException(exptn.getMessage());
			}
		}
		
		public override Dictionary<string, List<object>> getTokens ()
		{
			Dictionary<string, List<object>> ret = new Dictionary<string, List<object>> ();
			ret.Add("remove",new List<object>() {"rm","remove"});
			return ret;
		}
		
		public override List<KeyValuePair<string,initPointer>> getInitPointers()
		{
			List<KeyValuePair<string,initPointer>> ret = new List<KeyValuePair<string,initPointer>>();
			ret.Add(new KeyValuePair<string,initPointer>("remove",new initPointer(this.handleCLI)));
			return ret;
		}
		
		public void handleCLI(Dictionary<string,List<object>> tokens)
		{
			if (tokens["remove"].Count!=1){
				Console.WriteLine("Remove requires one path");
				return;
			}
			this.setupPaths(tokens);
			this.Execute();
		}
		
		public void setupPaths(Dictionary<string,List<object>> tokens)
		{
			Debug.WriteLine("SVN Remove path setup");
			WConly = this.checkPath((string)tokens["remove"][0]);
			if (WConly){
				filepath = new java.io.File((string)tokens["remove"][0]);
				urlpath[0] = null;
			}
			else{
				filepath = null;
				urlpath[0] = SVNURL.parseURIEncoded((string)tokens["remove"][0]);
			}
		}
		
		private bool checkPath(string inputPath)
		{
			// Checks if path is for repos access or WC access
			// Returns true for WC access (used for bool member WCOnly)
			Debug.WriteLine("SVN Remove method to check if input path is repos access");
			List<string> repos_access = new List<string>();
			// List of Repos access URLs
			repos_access.Add("file:///"); // Local repos
			repos_access.Add("http://");
			repos_access.Add("https://");
			repos_access.Add("svn://");
			repos_access.Add("svn+ssh://");
			
			foreach (string accessprefix in repos_access)
			{
				if (inputPath.StartsWith(accessprefix))
					return false;
			}
			
			return true;
		}
	}
}
